<template>
  <view class="container">
    <form @submit="submitForm">
      <!-- 表单内容 -->
      <view class="form-item">
        <text class="label">姓名：</text>
        <input class="input" v-model="formData.regName" placeholder="请输入姓名" />
        <text class="error" v-if="errors.regName">{{ errors.regName }}</text>
      </view>

      <view class="form-item">
        <text class="label">联系电话：</text>
        <input class="input" v-model="formData.regPhone" type="number" placeholder="请输入手机号" />
        <text class="error" v-if="errors.regPhone">{{ errors.regPhone }}</text>
      </view>

      <view class="form-item">
        <text class="label">职务：</text>
        <input class="input" v-model="formData.regPosition" placeholder="请输入职务信息" />
      </view>

      <view class="form-item">
        <text class="label">公司名称：</text>
        <input class="input" v-model="formData.regCompany" placeholder="请输入公司全称" />
      </view>

      <view class="form-item">
        <text class="label">行业领域：</text>
        <picker class="picker" @change="bindPickerChange" :value="hyLySelect" :range="industryOptions">
          <text>{{ industryOptions[industryIndex] || '请选择行业' }}</text>
        </picker>
      </view>

      <view class="form-item">
        <text class="label">是否用过直流接触器：</text>
        <view class="radio-group">
          <label class="radio-item">
            <radio 
              value="是" 
              :checked="formData.isUseProduct === true"
              @click="formData.isUseProduct = 1"/> 是
          </label>
          <label class="radio-item">
            <radio 
              value="否"
              :checked="formData.isUseProduct === false"
              @click="formData.isUseProduct = 0"/> 否
          </label>
        </view>
      </view>
      <button form-type="submit" class="submit-btn">提交信息</button>
    </form>
  </view>
</template>

<script>
import {  saveRegistration } from '@/api/registration'
export default {
  data() {
    return {
      formData: {
        regName: '',
        regPhone: '',
        regPosition: '',
        regCompany: '',
        hyLySelect: -1,
        industry: '',
        isUseProduct: 0
      },
      errors: {},
      industryIndex: -1,
      industryOptions: [
        '车辆', '通讯设备', '风能设备', '绞盘',
        '清洁设备', '液压动力设备', '电源设备', '电池', '其它'
      ]
    }
  },
  methods: {
    bindPickerChange(e) {
      this.industryIndex = e.detail.value
      this.formData.hyLySelect = this.industryIndex
      this.formData.industry = this.industryOptions[this.industryIndex]
    },
    validateForm() {
      this.errors = {}
      if (!this.formData.regName) this.errors.regName = '姓名不能为空'
      if (!/^1[3-9]\d{9}$/.test(this.formData.regPhone)) {
        this.errors.regPhone = '请输入有效的手机号码'
      }
      return Object.keys(this.errors).length === 0
    },
    // 原提交方法修改
    submitForm() {
      if (this.validateForm()) {
        // 模拟数据提交
        //uni.showLoading({ title: '提交中...' });
        saveRegistration(this.formData).then(() => {
          uni.hideLoading();
          uni.showToast({ title: '提交成功', icon: 'success' })
          this.showSupportTypeSelection();
        }).catch((err) => {
          console.error('提交失败:', err)
        })
        
        // setTimeout(() => {
        //   uni.hideLoading();
          
        // }, 1500);
      }
    },

    // 新增方法：显示支持类型选择
    showSupportTypeSelection() {
      uni.showActionSheet({
        title: '请选择服务类型',
        itemList: ['销售支持', '技术咨询'],
        success: (res) => {
          this.showContactMethodSelection(res.tapIndex === 0 ? '销售' : '技术');
        }
      });
    },

    // 新增方法：显示联系方式选择
    showContactMethodSelection(type) {
      uni.showActionSheet({
        title: `请选择${type}联系方式`,
        itemList: ['会话客服', '电话联系'],
        success: (res) => {
          if (res.tapIndex === 0) {
            // 打开微信客服会话
            uni.openCustomerServiceChat({
              success: () => {}// console.log('客服会话已打开')
            });
          } else {
            // 拨打电话
            uni.makePhoneCall({ phoneNumber: '13800138000' });
          }
        }
      });
    }
  }
}
</script>

<style>
.container {
  padding: 30rpx;
  background: #f5f5f5;
  min-height: 100vh;
}
form{
  border-radius: 12rpx;
}

.form-item {
  background: white;
  padding: 20rpx;
}

.label {
  font-size: 28rpx;
  color: #333;
  display: block;
  margin-bottom: 15rpx;
}

.input {
  height: 80rpx;
  font-size: 28rpx;
  border-bottom: 1rpx solid #eee;
  padding: 0 15rpx;
}

.picker {
  padding: 20rpx 0;
  color: #666;
}

.radio-group {
  display: flex;
  gap: 40rpx;
  margin-top: 20rpx;
}

.radio-item {
  display: flex;
  align-items: center;
  font-size: 28rpx;
}

.submit-btn {
  border-radius: 12rpx; /* 统一为直角 */
  height: 80rpx; /* 统一高度 */
  line-height: 80rpx; /* 垂直居中 */
  background: #007AFF;
  color: #fff;
}

.error {
  color: #e4393c;
  font-size: 24rpx;
  margin-top: 10rpx;
  display: block;
}
</style>